﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Security.Cryptography;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;

/// <summary>
/// Summary description for HelpUtil
/// </summary>
public class HelpUtil
{
    public static string GetMD5(string chuoi)
    {
        string str_md5 = "";
        byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

        MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
        mang = my_md5.ComputeHash(mang);

        foreach (byte b in mang)
        {
            str_md5 += b.ToString("X2");
        }

        return str_md5;
    }
    public static bool IsNotLogin(Page p)
    {
        if (p.Session[HelpConstEnum.ADMIN_IS_LOGIN_SESSION] != null && p.Session[HelpConstEnum.ADMIN_IS_LOGIN_SESSION].ToString() == "Y" && UserLogin.IsLogin(p)) return false;
        UserLogin.RemoveLogin(p);
        p.Session[HelpConstEnum.ADMIN_PRE_URL_SESSION] = p.Request.Url;
        p.Response.Redirect("" + "Login.aspx");
        return true;
    }
    public static object[] GetSimpleCKEditorConfig(Page P)
    {
        try
        {
            string path = P.Server.MapPath("~/App_Config/CKEditorToolBarSimple.txt");
            string[] strArray = File.ReadAllLines(path);
            if (strArray != null && strArray.Length > 0)
            {
                List<object> list = new List<object>();
                foreach (string str in strArray)
                {
                    if (str.Trim().Length == 0 || str.Trim().StartsWith("//"))
                        continue;
                    list.Add(str.Split(' '));
                }
                return list.ToArray();
            }
        }
        catch
        {
        }
        return new object[]{
            new object[] { "Bold", "Italic","Underline", "-", "NumberedList", "BulletedList", "-", "Link", "Unlink", "-"},
            new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print"},
            new object[] { "Cut", "Copy", "Paste", "PasteText", "PasteFromWord", "-", "Print"},
            new object[]{"Maximize"}
            };


    }
    public static string GetCKEditorUiColorUser(Page P)
    {
        try
        {
            string path = P.Server.MapPath("~/App_Config/CKEditorUiColorUser.txt");
            string[] strArray = File.ReadAllLines(path);
            if (strArray != null && strArray.Length > 0)
            {
                List<object> list = new List<object>();
                foreach (string str in strArray)
                {
                    if (str.Trim().Length == 0 || str.Trim().StartsWith("//"))
                        continue;
                    return str;
                }
              
            }
        }
        catch
        {
        }
        return "#4174dd";

    }
    public static string GetCKEditorUiColorAdmin(Page P)
    {
        try
        {
            string path = P.Server.MapPath("~/App_Config/CKEditorUiColorAdmin.txt");
            string[] strArray = File.ReadAllLines(path);
            if (strArray != null && strArray.Length > 0)
            {
                List<object> list = new List<object>();
                foreach (string str in strArray)
                {
                    if (str.Trim().Length == 0 || str.Trim().StartsWith("//"))
                        continue;
                    return str;
                }

            }
        }
        catch
        {
        }
        return "#4b6c9e";

    }
    public static decimal ParseDecimal(object agr)
    {
        if (agr == null)
        {
            return 0M;
        }
        if (agr is decimal)
        {
            return (decimal)agr;
        }
        decimal num = -79228162514264337593543950335M;
        try
        {
            if (agr.ToString() == "")
            {
                return 0M;
            }
            num = decimal.Parse(agr.ToString());
        }
        catch
        {
        }
        return num;
    }

 
    public static bool IsLoaiBaiVietTuKH(string idLoai)
    {
        if (idLoai == HelpConstEnum.LoaiBaiViet.Y_KIEN_KHACH_HANG.GetHashCode().ToString()
            || idLoai == HelpConstEnum.LoaiBaiViet.LUU_BUT_KHACH_HANG.GetHashCode().ToString())
            return true;
        return false;
    }
    public static void DeleteFile(Page page, object virtualPath)
    {
        try
        {
            if (virtualPath != null && virtualPath.ToString().Length > 0)
                File.Delete(page.Server.MapPath(virtualPath.ToString()));
        }
        catch
        {
        }
    }
    public static Image ReSizeImage(Image srcImage, int newWidth, int newHeight)
    {
        Bitmap newImage = new Bitmap(newWidth, newHeight);
        using (Graphics gr = Graphics.FromImage(newImage))
        {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(srcImage, new Rectangle(0, 0, newWidth, newHeight));
        }
        return newImage;
    }
}